package queries;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import entitys.Customer;
import enums.Result;

public class AddClientQuery {

	/**
	 * Method for updating database.
	 * This method gets a customer and adds it do database.
	 */
	public static Result exe(Customer c,java.sql.Connection conn)
	{
		Statement stmt1;
		Statement stmt;
		ResultSet res = null; 
		try {
			stmt1 = conn.createStatement();
			res = stmt1.executeQuery("SELECT * from ccrm.customer WHERE customer.id = '"+c.getId()+"'; ");
			if(res.next())
			{
				return Result.FAILED;
			}
			stmt = conn.createStatement();
			stmt.executeUpdate("INSERT INTO `ccrm`.`customer` (`id`, `firstName`, `lastName`, `phone`, `email`, `address`, `permit`,`birthdate`) VALUES ('"+c.getId()+"', '"+c.getFirstName()+"', '"+c.getLastName()+"', '"+c.getPhone()+"', '"+c.getEmail()+"', '"+c.getAddress()+"', '"+((c.getPermit())?1:0)+"', '"+c.getBirthdate()+"');");
			stmt.executeUpdate("DELETE FROM `ccrm`.`externalcustomer` WHERE `id`='"+c.getId()+"';");
			return Result.OK;
		} 
		catch (SQLException e) {
			e.printStackTrace();
			return Result.ERROR;
		}
	}
	/**
	 * Method for updating database.
	 * This method gets a customer and catagories in the form of ArrayList and adds to DB.
	 */
	public static Result exeCat(ArrayList<String> cat,java.sql.Connection conn)
	{
		Statement stmt;
		String id = cat.get(0);
		try {
			stmt = conn.createStatement();
			for(int i=1;i<cat.size();i++)
			{
				stmt.executeUpdate("INSERT INTO `ccrm`.`customercategory` (`custid`, `catid`) VALUES ('"+id+"', '"+cat.get(i)+"');");
			}
			return Result.OK;
		} 
		catch (SQLException e) {
			e.printStackTrace();
			return Result.ERROR;
		}
	}
}
